<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑水费记录-水费交款</title>
  </head>
  <body>
    <script type="text/javascript">
      Ext.onReady(function(){
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        //设置对话框最大宽度
        Ext.Msg.getDialog().setWidth(300);

        var waterLastYear ;
        var waterLastMonth ;
        
        var houseId = new Ext.form.Hidden({
          name: 'house_id',
          value: '<%=@house.id%>'
        });

        var waterId = new Ext.form.Hidden({
          name: 'id'
        });

        var ownerNumber = new Ext.form.TextField({
          fieldLabel: '业主编号',
          allowBlank: false,
          name: 'number',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.number%>'
        });
        var owner = new Ext.form.TextField({
          fieldLabel: '业主姓名',
          allowBlank: false,
          name: 'owner',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.owner%>'
        });
        var waterLastDate = new Ext.form.TextField({
          fieldLabel: '交费结止',
          allowBlank: false,
          name: 'owner',
          anchor: '90%',
          readOnly:true,
          value:'<%=@house.water_pay_year%>年<%=@house.water_pay_month%>月'
        });

        var waterBalance =  new Ext.form.TextField({
          fieldLabel: '上次余额',
          allowBlank: false,
          name: 'balance',
          anchor: '90%',
          readOnly:true
        });

        //上次抄表数
        var lastWaterMeter =  new Ext.form.NumberField({
          fieldLabel: '上次抄表数',
          allowBlank: false,
          name: 'last_meter',
          readOnly: true,
          anchor: '90%'
        });

        //本次抄表数
        var currentWaterMeter =  new Ext.form.NumberField({
          fieldLabel: '本次抄表数',
          allowBlank: false,
          name: 'current_meter',
          allowDecimals: false,
          allowNegative: false,
          enableKeyEvents:true,
          anchor: '90%'
        });

        //本次用水量
        var waterQuantity =  new Ext.form.NumberField({
          fieldLabel: '本次用水量',
          allowBlank: false,
          name: 'water_quantity',
          readOnly: true,
          anchor: '90%'
        });
        //本次金额
        var waterAmount =  new Ext.form.NumberField({
          fieldLabel: '本次金额',
          allowBlank: false,
          name: 'amount',
          anchor: '90%',
          readOnly:true
        });        
        
        currentWaterMeter.on('keyup',countWaterFee);
        //输入非常快时，keyup也不安全。
        currentWaterMeter.on('blur',countWaterFee);
        //抄表时间
        var recordingDate =  new Ext.form.DateField({
          fieldLabel: '抄表日期',
          name: 'meter_recording_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-18",
          allowBlank: true,
          blankText: "抄表日期必须选择",
          emptyText: '请选择日期...',
          anchor: '90%'
        });

        var waterCurrentYear = new Ext.form.ComboBox({ //combobox
          name: 'current_year',
          fieldLabel: '本次交费年',
          width: '90',
          resiable: true,
          typeAhead: true,
          allowBlank: false,
          handleHeight: 100, //
          displayField: 'key',
          valueField: 'value',
          mode: 'local',
          selectOnFocus: true,
          triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
          readOnly: true,
          emptyText: '选择年...',
          store: new Ext.data.Store({
            data: [['2004', '2004'],['2005', '2005'],['2006', '2006'],['2007', '2007'],['2008', '2008'], ['2009', '2009'], ['2010', '2010'], ['2011', '2011'], ['2012', '2012']],
            reader: new Ext.data.ArrayReader({
              id: 0
            }, [{
                name: 'value'
              }, {
                name: 'key'
              }])
          }), //数据储存到这个comboBox
          anchor: '90%'
        });
        waterCurrentYear.setValue(new Date().clearTime(true).getFullYear());
        var waterCurrentMonth = new Ext.form.ComboBox({ //combobox
          name: 'current_month',
          fieldLabel: '月',
          width: '90',
          resiable: true,
          typeAhead: true,
          allowBlank: false,
          handleHeight: 100, //
          displayField: 'key',
          valueField: 'value',
          selectOnFocus: true,
          mode: 'local',
          triggerAction: 'all',//这个属性非常重要,可以在选择一个值以后,依然还显示其它的值.
          readOnly: true,
          emptyText: '选择月...',
          store: new Ext.data.Store({
            data: [['1', '1'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'],['7', '7'], ['8', '8'], ['9', '9'], ['10', '10'], ['11', '11'], ['12', '12']],
            reader: new Ext.data.ArrayReader({
              id: 0
            }, [{
                name: 'value'
              }, {
                name: 'key'
              }])
          }), //数据储存到这个comboBox
          anchor: '90%'
        });

        //本次单价
        var waterUnitPrice =  new Ext.form.NumberField({
          fieldLabel: '本次单价',
          allowBlank: false,
          name: 'unit_price',
          anchor: '90%'
        });

        //付款方式
        var paymentMethod = new Ext.form.RadioGroup({
          //width : 220,
          fieldLabel: '付款方式',
          //hideLabel:true,
          readOnly : true,
          disabled : true,
          name:'payment_method',
          columns : 3,
          anchor: '90%',
          vertical :false,
          items:[
            {boxLabel:'余额',inputValue:'余额',name:'payment_method'},
            {boxLabel:'现金',inputValue:'现金',name:'payment_method'},
            {boxLabel:'混合',inputValue:'混合',name:'payment_method'}
          ]
        });
        //提交按钮
        var editWaterFeeButton = new Ext.Button({
          text: '提交',
          iconCls: 'icon-save',
          handler: function(){
            editWaterFeeButton.setDisabled(true);
            if (editWaterFeeRecordFormPanel.form.isValid()) {
              //判断一下抄表数是不是比上一次表数还小。
              if(lastWaterMeter.getValue() >= currentWaterMeter.getValue()){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '抄表数小于上一次表数！有误，请核实',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                editWaterFeeButton.enable();
                return;
              }
              //日期结止是不是比上一次结止还小。
              if(waterCurrentYear.getValue() < <%=@house.water_pay_year%>){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次结止年份小于上次结止日期！有误，请核实!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                editWaterFeeButton.enable();
                return;
              }else if(waterCurrentMonth.getValue() < <%=@house.water_pay_month%>){
                Ext.MessageBox.show({
                  title: '提示消息',
                  msg: '本次结止月份小于上次结止日期！有误，请核实!',
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.INFO
                });
                editWaterFeeButton.enable();
                return;
              }
              //如果是用余额付款，要计算一下余额是否够。
              if(waterAmount.getValue() > waterBalance.getValue()){
                if(paymentMethod.getValue().getGroupValue() ==  "余额"){
                  Ext.MessageBox.show({
                    title: '提示消息',
                    msg: '余额不足,请充值后再交款，或选择其它的支付方式。',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                  });
                  editWaterFeeButton.enable();
                  return;
                }
              }else{
                if(paymentMethod.getValue().getGroupValue() !=  "余额"){
                  Ext.MessageBox.show({
                    title: '提示消息',
                    msg: '您当前的余额大于要支付的金额，请选择其它支付方式。',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                  });
                  paymentMethod.reset();
                  editWaterFeeButton.enable();
                  return;
                }
              }

              editWaterFeeRecordFormPanel.form.submit({
                success: function(result, resp){
                  if (resp.result.info.indexOf("成功") >= 0) {
                    editWaterFeeRecordWin.destroy();
                  } else {
                    Ext.MessageBox.alert('消息', resp.result.info);
                    editWaterFeeButton.enable();
                  }
                },
                failure: function(result, request){
                  editWaterFeeButton.enable();
                  Ext.MessageBox.alert('消息', '服务器的通信失败!');
                }
              });
            }
            else {
              editWaterFeeButton.enable();
              //Ext.MessageBox.alert('输入验证有错', '请输入必输选项');
            }
          }
        })
        var editWaterFeeRecordFormPanel = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          labelWidth: 65,
          width: '400',
          labelAlign: 'right',
          frame : true,
          url: '/fee/updateWaterFeeRecord',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'house_id',
              mapping: 'house.id'
            },{
              name: 'number',
              mapping: 'house.number'
            },{
              name: 'owner',
              mapping: 'house.owner'
            },{
              name: 'area',
              mapping: 'house.area'
            },{
              name: 'unit_price'
            },{
              name: 'building_type',
              mapping: 'house.sub_type'
            },{
              name:'balance',
              mapping:'last_balance'
            },{
              name:'amount'
            },{
              name:'action_date'
            },{
              name:'last_year'
            },{
              name:'last_month'
            },{
              name:'current_year'
            },{
              name:'current_month'
            },{
              name:'last_meter'
            },{
              name:'current_meter'
            },{
              name:'water_quantity',
              mapping:'quantity'
            },{
              name:'payment_method'
            },{
              name:'action_date'
            },{
              name:'created_at'
            },{
              name:'meter_recording_date',
              type: 'date',
              dateFormat: 'Y/m/d'
            },{
              name:'unit_price'
            }]),
          items: [{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [ownerNumber]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [owner]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterLastDate]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterBalance]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [lastWaterMeter]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [currentWaterMeter]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterQuantity]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [recordingDate]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterCurrentYear]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterCurrentMonth]
                }]
            },{
              layout: 'column',
              border: false,
              items: [{
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterUnitPrice]
                }, {
                  columnWidth: .5,
                  layout: 'form',
                  border: false,
                  items: [waterAmount]
                }]
            },paymentMethod,houseId,waterId],
          buttons: [editWaterFeeButton,{
              text: '关闭',
              iconCls: 'exit',
              handler: function(){
                editWaterFeeRecordWin.destroy();
              }
            }]
        });
        editWaterFeeRecordFormPanel.render('edit_water_form');
        editWaterFeeRecordFormPanel.form.load({
          url: '/fee/getWaterById?water_id=<%=@waterId%>',
          waitMsg: '正在载入数据...',
          success: function(form, response){
            waterLastYear = parseInt(response.result.data.last_year);
            waterLastMonth = parseInt(response.result.data.last_month);
            waterLastDate.setValue(waterLastYear+"年"+waterLastMonth+"月");
            //alert(liftCurrentMonth.getValue());
          },
          failure: function(form, response){
            Ext.MessageBox.alert('提示信息', '信息加载失败');
          }
        });
        function countWaterFee(){
          //alert(waterAmount);
          waterQuantity.setValue(currentWaterMeter.getValue()-lastWaterMeter.getValue());
          waterAmount.setValue(((waterQuantity.getValue()*10)*(waterUnitPrice.getValue()*10))/100);
        };        
      })
    </script>
    <div id="edit_water_form"></div>
  </body>
</html>
